<?php


namespace app\cuisine\controller;


use app\cuisine\model\ImageModel;
use app\cuisine\model\IndexModel;
use app\cuisine\validate\ImageValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class ImageController extends AdminBaseController
{
    //显示页面
    public function index(){
        $list = ImageModel::order('list_order','DESC')->paginate(10);
        return $this->fetch('index',[
            'list'=>$list
        ]);
    }
//    //显示banner图-轮播图
//    public function banner(){
//        return "<a href='add'>upload</a>";
//    }
//排序
    public function Orders()
    {
        $model = new ImageModel();
        parent::listOrders($model); // TODO: Change the autogenerated stub
        $this->success('排序成功');
    }
    //上传图片
    public function add(){
        $list = ImageModel::select();
//        $this->assign('list');
        return $this->fetch('add',[
            'list'=>$list,
        ]);
    }
    public function addpost(){
        $image = input('name');
        $url1 = input('url');
        $url = cmf_get_image_url($image);
//        halt($url);
        //入库
        if(ImageModel::create(['image' => $url,'url'=>$url1])){
            $this->success('添加成功','Image/index');
        }else{
            $this->error('添加失败');
        }
    }
    //编辑
    public function edit(){
        $id = input('id');
        $list = ImageModel::find($id);
        return $this->fetch('edit',[
            'list' => $list
        ]);
    }
    public function editPost(){
        $id = input('id');
        $image = input('name');
        $url1 = input('url');
        $url = cmf_get_image_url($image);
        //查询
        $list = ImageModel::find($id);
        if(!$list){
            $this->error('未查询的记录');
        }
        //入库
        if($list->update(['image' => $url,'url'=>$url1],['id'=>$id])){
            $this->success('保存成功','Image/index');
        }else{
            $this->error('保存失败');
        }
    }
    //单条删除
    public function delete(){
        $id = input('id/d','');
        $isExist = ImageModel::find($id);
        //判断该id是否存在
        if(!$isExist){
            $this->error('该数据不存在');
        }
        $del = ImageModel::destroy($id);
        if($del){
            $this->success('删除成功','Image/index');
        }else{
            $this->error('删除失败');
        }
    }
}